import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCPracticeDelete {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        //1.创建一个数据源
        DataSource dataSource = new MysqlDataSource();
        //2.建立数据库的连接
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java107_2?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("12345");
        Connection connection = dataSource.getConnection();//只有设立好了数据库需要的信息才能创建一个连接
        //3.从控制台录入用户输入的内容
        System.out.println("请输入id：");
        int id = scanner.nextInt();
        //4.创建sql语句
        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);//进行预编译
        statement.setInt(1,id);//把第一个占位符设置成用户输入的id
        //执行sql语句
        int ret = statement.executeUpdate();//是指影响的行数
        System.out.println("ret = " + ret);
        //释放必要的资源
        statement.close();
        connection.close();
    }
}
